/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.StatusEvent.Type;
import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * <p>
 * Status of the application, stored as member data
 * 
 * <p> 
 * Every setting should fire a {@link StatusListener#onStatusUpdate(StatusEvent)}
 */
public class AppStatus {
	
	/** The app */
	private SolidalApp app;
	
	/** Status of the network */
	private NetworkUtils.Status networkStatus;
	
	/** Id of registration to GCM */
	private String gcmId;

	/** Current location, as obtained from location services */
	private Location location;
	
	/**
	 * Constructor
	 * 
	 * @param app
	 * 				the app
	 */
	public AppStatus(SolidalApp app) {
		this.app = app;
		init();
	}

	/** Init status */
	public void init() {
	    ConnectivityManager conn = (ConnectivityManager)
	            app.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo info = conn.getActiveNetworkInfo();
		networkStatus = NetworkUtils.getStatus(info);
		gcmId = null;//app.getSharedPreferences(app.getClass().getCanonicalName(), Context.MODE_PRIVATE)
				//.getString(Constants.REGISTRATION_ID, null);
		location = null;
	}
	
	public NetworkUtils.Status getNetworkStatus() {
		return networkStatus;
	}
	
	public void setNetworkStatus(NetworkInfo info) {
		setNetworkStatus(NetworkUtils.getStatus(info));
	}
	
	public void setNetworkStatus(NetworkUtils.Status networkStatus) {
		if(this.networkStatus!=networkStatus) {
			this.networkStatus = networkStatus;
			app.fireStatusUpdate(new StatusEvent(Type.NETWORK));
		}
	}
	
	public String getGcmId() {
		return gcmId;
	}
	
	public void setGcmId(String gcmId) {
		if("".equals(gcmId))
			gcmId=null;
		this.gcmId = gcmId;
		app.fireStatusUpdate(new StatusEvent(Type.GCM));
	}
	
	public Location getLocation() {
		return location;
	}
	
	public void setLocation(Location location) {
		if(this.location!=location) {
			this.location = location;
			app.fireStatusUpdate(new StatusEvent(Type.LOCATION));
			if(app.getData(DataKey.ADDRESS_LOCATION_MANUAL)==null)
				app.setData(DataKey.ADDRESS_LOCATION, location);
		}
	}
	
	/**
	 * Create a new {@link Location} object given coordinates using the
	 * same settings of the current location 
	 * 
	 * @param latitude
	 * 		new latitude
	 * @param longitude
	 * 		new longitude
	 * @return
	 * 		the new location object, or null if no location available
	 */
	public Location newLocation(double latitude, double longitude) {
		Location ret = new Location(location);
		if (ret!=null) {
			ret.setLatitude(latitude);
			ret.setLongitude(longitude);
		}
		return ret;
	}

}
